package dao;

import pojo.Chess;
import pojo.Player;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class QiziDao {

    public static void main(String[] args) {
//        insertChess(new Chess("🏀",0));
//        insertChess(new Chess("⚽",0));

//        System.out.println("星形字符: ★ ☆ ✮ ✩ ✰ ❋");
//        System.out.println("圆形字符: ● ○ ◉ ◎ ⚫ ⚪ ❍");
//        System.out.println("方形字符: ■ □ ▣ ▢ ▪ ▫");
//        System.out.println("心形字符: ♥ ♡ ❤");
//        System.out.println("箭头字符: → ← ↑ ↓ ↔ ↕ ⇨");
//        System.out.println("装饰性字符: ❖ ✿ ✩ ☀ ☁ ☂ ☃");
//        System.out.println("数学符号: √");
//        System.out.println("复古装饰符号: ✄ ✁ ❖ ✩");
//        System.out.println("植物符号: ☘ ❀ ✿ ❁ ⚘");
//        System.out.println("天气符号: 🌞 🌈 ⛅ 🌧 ⛈ 🌨 🌩");
//        System.out.println("权力符号: ⚡ ⚒ ⚖ ⚔ 🛡 🗡 🚨");
//        System.out.println("表情符号: 🦄 👑 🌟 🌸 💫 💪 🎨 🎭 🎲 🎮");
//        System.out.println("物体符号: 🚀 ✈ 🚗 🚲 🛵 🚂 🛳");
//        System.out.println("动植物符号: 🌵 🌾 🌳 🌷 🌻 🐶 🐱 🐰 🦊");
//        System.out.println("食物符号: 🍎 🍓 🍕 🍔 🍿 🍩 🍪 🥗 🥩 🍇");
//        System.out.println("运动符号: ⚽ 🏀 🏈 🎾 🏌 🚴 ⛷ 🏊");

//        insertChess(new Chess("🍎",5));
//        insertChess(new Chess("🍕",5));
//        insertChess(new Chess("\uD83C\uDF33",10));
//        insertChess(new Chess("\uD83C\uDFBE",15));
//        insertChess(new Chess("\uD83C\uDF38",15));
//        insertChess(new Chess("\uD83C\uDFA8",15));
//
//        insertChess(new Chess("\uD83D\uDC36",25));
//        insertChess(new Chess("\uD83C\uDFAE",25));
//        insertChess(new Chess("\uD83D\uDE80",25));

        for (Chess chess : chessList) {
            System.out.println(chess);
        }
//        List<Chess> allQizi = getAllQizi();
//        for (Chess chess : allQizi) {
//            System.out.println(chess);
//        }
    }

    //文件[qizi]中所有的棋子都存在此集合中 //注意棋子id从1开始
    private static List<Chess> chessList=getAllQizi();
    //文件[qizi]的位置
    private static final String filePath="src/main/resources/qizi";
    /*
    负责人：李季岭
    功能：文件[qizi]的所有json对象转为Chess对象，返回Chess对象集合
    参数值：void
    返回值：Chess对象集合
    */
    public static List<Chess> getAllQizi(){
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))){
            chessList=(List<Chess>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            return new ArrayList<>();
        }

        return chessList;
    }

    /*
        负责人：李季岭
        功能：根据棋子id返回对应的Chess对象
        参数：id 要查找的棋子id
        返回值：返回Chess对象
     */
    public static Chess findQizi(int id) {
        for (Chess chess : chessList) {
            if (chess.getId()==id){
                return chess;
            }
        }
        return null;
    }

    /*
    功能：将Chess对象写入文件[qizi]中
         注意：1.要将chess的id属性赋值
              2.添加到Chess集合
              3.写入文件
    参数：chess 棋子对象
    返回值：void
 */
    public static void insertChess(Chess chess){
        chess.setId(chessList.size()+1);
        chessList.add(chess);

        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))){
            oos.writeObject(chessList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }



}
